<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="友情链接管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="标题" prop="title">
                <el-input v-model="formData.title"/>
            </el-form-item>
            <el-form-item label="链接" prop="url">
                <el-input v-model="formData.url"/>
            </el-form-item>
            <el-form-item label="缩略图" prop="thumbnail">
                <el-upload
                    class="avatar-uploader"
                    :action="apiHost + '/file/upload'"
                    :show-file-list="false"
                    :on-success="handleSuccess"
                    :headers="headers"
                >
                    <div v-if="formData.thumbnail">
                        <img :src="formData.thumbnail" class="avatar">
                    </div>
                    <i v-else class="el-icon-plus avatar-uploader-icon"/>

                </el-upload>
            </el-form-item>

            <el-form-item>
                <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { LinkApi } from '@/api/cms/Link'
import { getToken } from '@/utils/auth'

export default {
    name: 'CmsLinkUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                title: '',
                thumbnail: '',
                url: ''
            },
            rules: {
                title: [
                    { required: true, message: '请输入文章标题', trigger: 'blur' },
                    { min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur' }
                ],
                url: [
                    { required: true, message: '请输入链接', trigger: 'blur' }
                ]
            },
            dialogVisible: false,
            apiHost: process.env.VUE_APP_BASE_API,
            headers: { 'X-Token': getToken() }
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        async showUpdate(data) {
            const that = this

            that.dialogVisible = true
            if (that.$refs['formRef'] !== undefined) {
                that.$refs['formRef'].resetFields()
            }

            that.$nextTick(() => {
                if (data.id > 0) {
                    that.formData = Object.assign({}, data)
                } else {
                    that.formData.id = 0
                }
            })
        },
        handleSuccess(e) {
            this.formData.thumbnail = e.data
        },

        // 提交表单
        submitForm() {
            const that = this

            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        LinkApi.edit(that.formData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        LinkApi.add(that.formData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>

<style>
</style>
